﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GlobalGameJam
{
    public class CollisionManager
    {
        public List<Plateform> plateforms;
        public List<Plateform> plateforms2;
        public CollisionManager()
        {
            plateforms = new List<Plateform>();
            plateforms2 = new List<Plateform>();
        }
        public bool IntersectPixels(Rectangle rectangleA, Color[] dataA,
    Rectangle rectangleB, Color[] dataB)
        {
            // Find the bounds of the rectangle intersection
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            // Check every point within the intersection bounds
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
                    // Get the color of both pixels at this point
                    Color colorA = dataA[(x - rectangleA.Left) +
                    (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) +
                    (y - rectangleB.Top) * rectangleB.Width];

                    // If both pixels are not completely transparent,
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        // then an intersection has been found
                        return true;
                    }
                }
            }

            // No intersection found
            return false;
        }
        public bool thereIsObstacle(Player player)
        {

            foreach (Plateform p in plateforms)
                foreach (Rectangle rec in player.hitbox)
                {
                    foreach (Rectangle rec2 in p.hitbox)
                    {
                        Color[] personTextureData = player.sprite.data;
                        // Ca bouffe toutes lees ressoucres batatrd
                        Color[] plateformTextureData = p.data;

                        if (rec.Intersects(rec2) && IntersectPixels(rec, personTextureData, rec2, plateformTextureData))
                        {
                            if (p.collide())
                                return true;
                        }
                    }
                }
            return false;
        }
        public Plateform getObstacle(Player player)
        {
            foreach (Plateform p in plateforms)
                foreach (Rectangle rec in player.hitbox)
                {
                    foreach (Rectangle rec2 in p.hitbox)
                    {
                        Color[] personTextureData =
new Color[player.sprite._sprite.Width * player.sprite._sprite.Height];
                        Color[] plateformTextureData =
new Color[p.sprite.Width * p.sprite.Height];
                        player.sprite._sprite.GetData(personTextureData);
                        p.sprite.GetData(plateformTextureData);
//                        if(rec.Intersects(rec2))
                        if (rec.Intersects(rec2) && IntersectPixels(rec, personTextureData, rec2, plateformTextureData))
                            return p;
                    }
                }
            throw (new System.Exception("no obstacle"));
        }
        public bool tryToEscalade(Player p)
        {
            p.position.Y -= 1;
            p.UpdateHitbox();

            for (int i = 0; i < p.scalePower; i++, p.position.Y--, p.UpdateHitbox())
                if (!this.thereIsObstacle(p))
                {
                    p.position.Y += i + 1;
                    allDown(p.scalePower);
                    return true;
                }
            p.position.Y += p.scalePower + 1;
            return false;
        }
        public void allLeft(float amount)
        {
            foreach (Plateform p in plateforms)
                p.position.X -= amount;
            foreach (Plateform p in plateforms2)
                p.position.X -= amount;
        }
        public void allRight(float amount)
        {
            foreach (Plateform p in plateforms)
                p.position.X += amount;
            foreach (Plateform p in plateforms2)
                p.position.X += amount;
        }
        public void allDown(float amount)
        {
            foreach (Plateform p in plateforms)
                p.position.Y += amount;
            foreach (Plateform p in plateforms2)
                p.position.Y += amount;
        }
        public void allUp(float amount)
        {
            foreach (Plateform p in plateforms)
                p.position.Y -= amount;
            foreach (Plateform p in plateforms2)
                p.position.Y -= amount;
        }
    }
}
